package com.dinghui.ebookbag.module.middle.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import com.dinghui.ebookbag.R;
import com.dinghui.ebookbag.enity.ChildData;
import com.dinghui.ebookbag.enity.CurriculumCatalog;
import com.dinghui.ebookbag.enity.GroupData;

import java.util.List;

public class CurriculumCatalogAdapter2 extends BaseExpandableListAdapter {

//    private Context mContext;
//    private List<GroupData> mGroupData;
//    private List<List<ChildData>> mChildData;
//
//    public CurriculumCatalogAdapter2(Context mContext, List<GroupData> mGroupData, List<List<ChildData>> mChildData) {
//        List<CurriculumCatalog>
//        this.mContext = mContext;
//        this.mGroupData = mGroupData;
//        this.mChildData = mChildData;
//    }


    private Context mContext;
    private List<CurriculumCatalog> mGroupData;
    private List<List<CurriculumCatalog>> mChildData;
    public CurriculumCatalogAdapter2(Context mContext, List<CurriculumCatalog> mGroupData, List<List<CurriculumCatalog>> mChildData) {
        this.mContext = mContext;
        this.mGroupData = mGroupData;
        this.mChildData = mChildData;
    }

    @Override
    public int getGroupCount() {
        int ret = 0;
        if (mGroupData != null) {
            ret = mGroupData.size();
        }
        return ret;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int ret = 0;
        if (mChildData != null) {
            ret = mChildData.get(groupPosition).size();
        }
        return ret;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChildData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_group_catalog, parent, false);
            holder = new ViewHolderGroup();
            holder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderGroup) convertView.getTag();
        }
        holder.tv_group_name.setText(mGroupData.get(groupPosition).getText());
        return convertView;
    }


    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item_catalog, parent, false);
            holder = new ViewHolderItem();
            holder.tv_item_name = convertView.findViewById(R.id.tv_child_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderItem) convertView.getTag();
        }
        holder.tv_item_name.setText(mChildData.get(groupPosition).get(childPosition).getText());
        return convertView;
    }

    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup{
        private TextView tv_group_name;
    }

    private static class ViewHolderItem{
        private TextView tv_item_name;
    }
}
